package cc.rengu.igas.channel.cncp.biz.impl;

import cc.rengu.igas.channel.cpcn.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cpcn.facade.CpcnMchntOrderService;
import cc.rengu.igas.channel.cpcn.facade.request.*;
import cc.rengu.igas.channel.cpcn.facade.response.*;
import cc.rengu.igas.channel.cpcn.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/7/16 14:53
 */
public class CpcnMchntOrderServiceImpl implements CpcnMchntOrderService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 商户订单支付查询
     *
     * @param cpcnMchntOrderPayQueryRequest
     * @return
     */
    @Override
    public Result<CpcnMchntOrderPayQueryResponse> cpcnMchntOrderPayQuery(CpcnMchntOrderPayQueryRequest cpcnMchntOrderPayQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnMchntOrderPayQueryResponse> result = new Result<>();
        CpcnMchntOrderPayQueryResponse cpcnMchntOrderPayQueryResponse = new CpcnMchntOrderPayQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(cpcnMchntOrderPayQueryRequest,
                    "CpcnMchntOrderPayQueryService", CpcnMchntOrderPayQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CpcnMchntOrderPayQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户订单支付查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cpcnMchntOrderPayQueryResponse.setRespDesc(((BizException) e).getCode());
                cpcnMchntOrderPayQueryResponse.setRespDesc(e.getMessage());
            } else {
                cpcnMchntOrderPayQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cpcnMchntOrderPayQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }


    /**
     * 中金支付商户订单付款账户信息查询
     *
     * @param cpcnMchntOrderPayerAcctInfoQueryRequest
     * @return
     */
    @Override
    public Result<CpcnMchntOrderPayerAcctInfoQueryResponse> cpcnMchntOrderPayerAcctInfoQuery(CpcnMchntOrderPayerAcctInfoQueryRequest cpcnMchntOrderPayerAcctInfoQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnMchntOrderPayerAcctInfoQueryResponse> result = new Result<>();
        CpcnMchntOrderPayerAcctInfoQueryResponse cpcnMchntOrderPayerAcctInfoQueryResponse = new CpcnMchntOrderPayerAcctInfoQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(cpcnMchntOrderPayerAcctInfoQueryRequest,
                    "CpcnMchntOrderPayerAcctInfoQueryService", CpcnMchntOrderPayerAcctInfoQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CpcnMchntOrderPayerAcctInfoQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("中金支付商户订单付款账户信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cpcnMchntOrderPayerAcctInfoQueryResponse.setRespDesc(((BizException) e).getCode());
                cpcnMchntOrderPayerAcctInfoQueryResponse.setRespDesc(e.getMessage());
            } else {
                cpcnMchntOrderPayerAcctInfoQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cpcnMchntOrderPayerAcctInfoQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 中金支付商户订单支付退款
     *
     * @param cpcnMchntOrderPayRefundRequest
     * @return
     */
    @Override
    public Result<CpcnMchntOrderPayRefundResponse> cpcnMchntOrderPayRefund(CpcnMchntOrderPayRefundRequest cpcnMchntOrderPayRefundRequest) {

        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnMchntOrderPayRefundResponse> result = new Result<>();
        CpcnMchntOrderPayRefundResponse cpcnMchntOrderPayRefundResponse = new CpcnMchntOrderPayRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(cpcnMchntOrderPayRefundRequest,
                    "CpcnMchntOrderPayRefundService", CpcnMchntOrderPayRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CpcnMchntOrderPayRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("中金支付商户订单支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cpcnMchntOrderPayRefundResponse.setRespDesc(((BizException) e).getCode());
                cpcnMchntOrderPayRefundResponse.setRespDesc(e.getMessage());
            } else {
                cpcnMchntOrderPayRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cpcnMchntOrderPayRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 中金支付商户订单支付退款查询
     *
     * @param cpcnMchntOrderPayRefundQueryRequest
     * @return
     */
    @Override
    public Result<CpcnMchntOrderPayRefundQueryResponse> cpcnMchntOrderPayRefundQuery(CpcnMchntOrderPayRefundQueryRequest cpcnMchntOrderPayRefundQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnMchntOrderPayRefundQueryResponse> result = new Result<>();
        CpcnMchntOrderPayRefundQueryResponse cpcnMchntOrderPayRefundQueryResponse = new CpcnMchntOrderPayRefundQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(cpcnMchntOrderPayRefundQueryRequest,
                    "CpcnMchntOrderPayRefundQueryService", CpcnMchntOrderPayRefundQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CpcnMchntOrderPayRefundQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("中金支付商户订单支付退款查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cpcnMchntOrderPayRefundQueryResponse.setRespDesc(((BizException) e).getCode());
                cpcnMchntOrderPayRefundQueryResponse.setRespDesc(e.getMessage());
            } else {
                cpcnMchntOrderPayRefundQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cpcnMchntOrderPayRefundQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 中金支付商户订单支付原路退款
     *
     * @param cpcnMchntOrderPayOriginalRefundRequest
     * @return
     */
    @Override
    public Result<CpcnMchntOrderPayOriginalRefundResponse> cpcnMchntOrderPayOriginalRefund(CpcnMchntOrderPayOriginalRefundRequest cpcnMchntOrderPayOriginalRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnMchntOrderPayOriginalRefundResponse> result = new Result<>();
        CpcnMchntOrderPayOriginalRefundResponse cpcnMchntOrderPayOriginalRefundResponse = new CpcnMchntOrderPayOriginalRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(cpcnMchntOrderPayOriginalRefundRequest,
                    "CpcnMchntOrderPayOriginalRefundService", CpcnMchntOrderPayOriginalRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CpcnMchntOrderPayOriginalRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("中金支付商户订单支付原路退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cpcnMchntOrderPayOriginalRefundResponse.setRespDesc(((BizException) e).getCode());
                cpcnMchntOrderPayOriginalRefundResponse.setRespDesc(e.getMessage());
            } else {
                cpcnMchntOrderPayOriginalRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cpcnMchntOrderPayOriginalRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
